﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SQLServer.DBUtility;
using Tenancy.Helper;

namespace WindowsServiceOrder
{
    public partial class Service1 : ServiceBase
    {
      
        public Service1()
        {
            InitializeComponent();
         
        }
        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            StartWXrefund();
        }

      
        private Thread ThreadWXrefund;

        private void StartWXrefund()
        {
            try
            {
                ThreadWXrefund = new Thread(new ThreadStart(WXrefund));
                ThreadWXrefund.Start();
                System.Diagnostics.Trace.Write("线程任务开始");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                throw ex;
            }
        }

        private void WXrefund()
        {
            while (true)
            {

                try
                {

                    string OrdersNums = string.Format("select OrderNum from OnlineOrders where CAST(DATEDIFF(HH,Creationtime,GETDATE()) as int) >2 and OrderStatus=0 ");
                    DataTable OrdersData = DbLocalSQL.Query(OrdersNums).Tables[0];
                    if (OrdersData != null && OrdersData.Rows.Count > 0)
                    {
                        string cgsSxm = string.Empty;
                        for (int i = 0; i < OrdersData.Rows.Count; i++)
                        {
                            cgsSxm += string.Format("'{0}',", OrdersData.Rows[i]["OrderNum"].ToString());
                        }
                        if (cgsSxm.EndsWith(","))
                        {
                            cgsSxm = cgsSxm.Remove(cgsSxm.Length - 1, 1);

                        }
                        string strsql = string.Format(@"update dbo.OnlineOrders set OrderStatus='2',CancelCategory='超时取消',CancelReason='超时取消'  where  OrderNum in ({0}) ",
                            cgsSxm);
                        if (DbLocalSQL.ExecuteSql(strsql) > 0)
                        {
                            for (int i = 0; i < OrdersData.Rows.Count; i++)
                            {
                                string SXMMMD = string.Format("select PeoType,SalemanTel from OnlineOrders where OrderNum='{0}'", OrdersData.Rows[i]["OrderNum"].ToString());
                                DataTable HUXMMD = DbLocalSQL.Query(SXMMMD).Tables[0];
                                if(HUXMMD!=null&&HUXMMD.Rows.Count>0)
                                {
                                    Tenancy.Helper.MsgSend.HB_SendMsg("18551127295","【利星行租赁】测试test服务"); ;
                                    //老用户给业务员，行政，区域经理推送
                                    if (HUXMMD.Rows[0]["PeoType"].ToString() == "业务员")
                                    {
                                        if(!string.IsNullOrEmpty(HUXMMD.Rows[0]["SalemanTel"].ToString()))
                                        {
                                            //给业务员推送
                                            Tenancy.Helper.MsgSend.HB_SendMsg(HUXMMD.Rows[0]["SalemanTel"].ToString(), GenerClass.CancelOrder(OrdersData.Rows[i]["OrderNum"].ToString()));
                                            string OldStr = string.Format("select * from SalesAdminManger where SalesTel='{0}'", HUXMMD.Rows[0]["SalemanTel"].ToString());
                                            DataTable OldData = DbLocalSQL.Query(OldStr).Tables[0];
                                            if (OldData != null && OldData.Rows.Count > 0)
                                             {
                                                 //给行政推送
                                                 if (!string.IsNullOrEmpty(OldData.Rows[0]["AdminTel"].ToString()))
                                                 {
                                                     Tenancy.Helper.MsgSend.HB_SendMsg(OldData.Rows[0]["AdminTel"].ToString(), GenerClass.CancelOrder(OrdersData.Rows[i]["OrderNum"].ToString()));
                                                 }

                                                 //给区域经理推送
                                                 if (!string.IsNullOrEmpty(OldData.Rows[0]["ManagerTel"].ToString()))
                                                 {
                                                     Tenancy.Helper.MsgSend.HB_SendMsg(OldData.Rows[0]["ManagerTel"].ToString(), GenerClass.CancelOrder(OrdersData.Rows[i]["OrderNum"].ToString()));
                                                 }
                                             }
                                        }
                                    }

                                    //新用户区域经理推送
                                    if (HUXMMD.Rows[0]["PeoType"].ToString() == "区域经理")
                                    {
                                        if (!string.IsNullOrEmpty(HUXMMD.Rows[0]["SalemanTel"].ToString()))
                                        {
                                            Tenancy.Helper.MsgSend.HB_SendMsg(HUXMMD.Rows[0]["SalemanTel"].ToString(), GenerClass.CancelOrder(OrdersData.Rows[i]["OrderNum"].ToString()));
                                        }
                                    }
                                }
                            
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.Write(ex.Message);
                    throw ex;
                }

                Thread.Sleep(60000);
            }
        }


        protected override void OnStop()
        {
            try
            {
                ThreadWXrefund.Abort();

                bool Flag = false;

                System.Diagnostics.Trace.Write("线程停止");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
            }
        }
    }
}
